package com.example.demo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.bumptech.glide.Glide;
import com.example.demo.API.TheItemList;
import com.example.demo.API.TheItemService;
import com.example.demo.API.TheItemsGson;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RecycleDome2Activity extends AppCompatActivity {
    private Context context;
    //private List<TheItems> apps = new ArrayList<TheItems>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycle_dome2);

        RecyclerView recycleView2 = findViewById(R.id.recycle_view2);

        context = this;

        //1.指定表现形式
        //列表
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);

        //网格  这两种常用
        //GridLayoutManager gridLayoutManager = new GridLayoutManager(context,2);

        //指定形式
        recycleView2.setLayoutManager(linearLayoutManager);

        //2.自定义适配器
        MyAdapterTest myAdapterTest = new MyAdapterTest();
        recycleView2.setAdapter(myAdapterTest);

        //网络请求
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://rap2api.taobao.org/app/mock/data/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        TheItemService service = retrofit.create(TheItemService.class);

        Call<TheItemsGson> call = service.getInfo();
        //Log.i("78----------", "636363");

        //同步请求

        //异步
        // http://rap2api.taobao.org/app/mock/data/2021537

        call.enqueue(new Callback<TheItemsGson>() {
            @Override
            public void onResponse(Call<TheItemsGson> call, Response<TheItemsGson> response) {
                TheItemsGson theItemList = response.body();
                myAdapterTest.setData(theItemList.getTheItemsList());

                System.out.println(theItemList.getTheItemsList().get(0).getTvDowloadCount());
                Log.i("onResponse","success");

            }

            @Override
            public void onFailure(Call<TheItemsGson> call, Throwable t) {
                //Log.i("onFailure","bad!!!");
                t.printStackTrace();
            }
        });

    }
}










